package devserv

import (
	"config"
	"encoding/json"
	"log"
	"math/rand"
	"time"
)

const (
	cid = "85133f6b-a0ec-4a47-a8a7-7779f6e91ba1"
)

// 测试净化器开关
func TestOpenOrBreak(action string) {
	log.Println("action: ", action)
	// if action != 0 && action != 1 {
	// 	log.Println("TestOpenOrBreak, please set action 0 or 1")
	// 	return
	// }
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	type Message struct {
		Uri    string `json:"uri"`
		Action string `json:"action"`
		Pwm    int    `json:"pwm"`
	}

	message := Message{}
	message.Uri = "/power"
	message.Action = action

	if action == "on" {
		message.Pwm = 4000
	}

	log.Println("message: ", message)

	buf, err := json.Marshal(message)
	if err != nil {
		log.Println("err: ", err)
	}

	// log.Println("test 1")

	// c := GetControllerById(cid)
	// if c == nil {
	// 	log.Printf("controller %s not exist", cid)
	// 	return
	// }

	// log.Println("test 2")

	err = c.SendMessage(buf)
	if err != nil {
		log.Println("err: ", err)
	}

	log.Println("test 3")
}

//测试净化器的风速设置，分静音、低、中、高，分别对应1,2,3,4
func TestSetSpeedLeve(leve int) {
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	if leve < 1 || leve > 4 {
		log.Println("TestOpenOrBreak, please set leve to 1, 2, 3")
		return
	}

	var pwm int
	if leve == 1 {
		pwm = c.MaxPwm / 3
	} else if leve == 2 {
		pwm = c.MaxPwm * 2 / 3
	} else if leve == 3 {
		pwm = c.MaxPwm
	} else if leve == 4 {
		pwm = c.MaxPwm / 5
	}

	type Message struct {
		Uri string `json:"uri"`
		Pwm int    `json:"pwm"`
	}

	message := Message{}
	message.Uri = "/setFanSpeed"
	message.Pwm = pwm

	buf, _ := json.Marshal(message)
	err := c.SendMessage(buf)
	if err != nil {

	}
}

func GetRuntime(action string) {
	log.Println("action: ", action)
	// if action != 0 && action != 1 {
	// 	log.Println("TestOpenOrBreak, please set action 0 or 1")
	// 	return
	// }
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	type Message struct {
		Uri    string `json:"uri"`
		Action string `json:"action"`
		Pwm    int    `json:"pwm"`
	}

	message := Message{}
	message.Uri = "/getRuntime"

	log.Println("message: ", message)

	buf, err := json.Marshal(message)
	if err != nil {
		log.Println("err: ", err)
	}

	// log.Println("test 1")

	// c := GetControllerById(cid)
	// if c == nil {
	// 	log.Printf("controller %s not exist", cid)
	// 	return
	// }

	// log.Println("test 2")

	err = c.SendMessage(buf)
	if err != nil {
		log.Println("err: ", err)
	}
}

// func TestMoon(action string) {
// 	// if leve < 1 || leve > 3 {
// 	// 	log.Println("TestOpenOrBreak, please set leve to 1, 2, 3")
// 	// 	return
// 	// }

// 	type Message struct {
// 		Uri    string `json:"uri"`
// 		Action string `json:"action"`
// 	}

// 	message := Message{}
// 	message.Uri = "/moon"
// 	message.Action = action

// 	buf, _ := json.Marshal(message)
// 	c := GetControllerById(cid)
// 	if c == nil {
// 		log.Printf("controller %s not exist", cid)
// 		return
// 	}

// 	err := c.SendMessage(buf)
// 	if err != nil {

// 	}
// }

func TestIon(action string) {
	// if leve < 1 || leve > 3 {
	// 	log.Println("TestOpenOrBreak, please set leve to 1, 2, 3")
	// 	return
	// }

	type Message struct {
		Uri    string `json:"uri"`
		Action string `json:"action"`
	}

	message := Message{}
	message.Uri = "/ion"
	message.Action = action

	buf, _ := json.Marshal(message)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {

	}
}

func TestAuto(action string) {
	// if leve < 1 || leve > 3 {
	// 	log.Println("TestOpenOrBreak, please set leve to 1, 2, 3")
	// 	return
	// }

	type Message struct {
		Uri    string `json:"uri"`
		Action string `json:"action"`
	}

	message := Message{}
	message.Uri = "/auto"
	message.Action = action

	buf, _ := json.Marshal(message)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {

	}
}

func TestUpdate() {
	// if leve < 1 || leve > 3 {
	// 	log.Println("TestOpenOrBreak, please set leve to 1, 2, 3")
	// 	return
	// }

	type UpgradeRequest struct {
		URI        string `json:"uri"`
		URL        string `json:"url"`
		NewVersion string `json:"newVersion"`
	}

	var ur = &UpgradeRequest{}
	ur.URI = "/upgrade"
	ur.NewVersion = config.LatestFirmVersion
	ur.URL = config.FirmDownloadUrl
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	log.Printf("update %s firm", cid)
	c.WriteJSONWait(ur, nil)
	//if config.LatestFirmVersionCode > c.FirmVersionCode {
	//	c.WriteJSONWait(ur, nil)
	//}
}

//测试净化器的无极风速设置， 0 至 100
func TestSetSpeed(speed int) {
	if speed < 0 || speed > 100 {
		log.Println("TestOpenOrBreak, leve is out of range")
		return
	}

	type Message struct {
		Uri   string `json:"uri"`
		Speed int    `json:"speed"`
	}

	message := Message{}
	message.Uri = "/setSpeed"
	message.Speed = speed

	buf, _ := json.Marshal(message)

	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {

	}
}

//测试净化器的滤网自清洁
func TestSetSelfClean(action int) {
	if action != 0 && action != 1 {
		log.Println("TestSetSelfClean, action < 0")
		return
	}

	type Message struct {
		Uri    string `json:"uri"`
		Action int    `json:"action"`
	}

	message := Message{}
	message.Uri = "/selfClean"
	message.Action = action

	buf, _ := json.Marshal(message)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {

	}
}

////测试净化器的儿童锁
func TestSetChildLock(action int) {
	if action < 0 {
		log.Println("TestSetSelfClean, action < 0")
		return
	}

	type Message struct {
		Uri    string `json:"uri"`
		Action int    `json:"action"`
	}

	message := Message{}
	message.Uri = "/childLock"
	message.Action = action

	buf, _ := json.Marshal(message)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {

	}
}

//测试净化器的auto设置
func TestAutoSet(action int) {
	if action < 0 {
		log.Println("TestAutoSet, action < 0")
		return
	}

	type Message struct {
		Uri    string `json:"uri"`
		Action int    `json:"action"`
	}

	message := Message{}
	message.Uri = "/autoSet"
	message.Action = action

	buf, _ := json.Marshal(message)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {

	}
}

func randNum() int {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	randNum := r.Intn(100000)
	if randNum < 0 {
		return -randNum
	}
	return randNum
}

//测试净化器的timer
func TestTimerO(duration int) {
	log.Println("TestTimer")
	type UserTimerInput struct {
		Uri      string `json:"uri"`
		Duration int    `json:"duration"`
		TimerId  int    `json:"id"`
		Action   int    `json:"action"`
	}

	plan := UserTimerInput{}
	plan.Uri = "/timer"
	plan.Duration = duration
	plan.TimerId = randNum()
	plan.Action = 1

	buf, _ := json.Marshal(plan)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {
		log.Println("err: ", err)
	}

}

//测试净化器的timer
func TestTimerC(duration int) {
	log.Println("TestTimer")
	type UserTimerInput struct {
		Uri      string `json:"uri"`
		Duration int    `json:"duration"`
		TimerId  int    `json:"id"`
		Action   int    `json:"action"`
	}

	plan := UserTimerInput{}
	plan.Uri = "/timer"
	plan.Duration = duration
	plan.TimerId = randNum()
	plan.Action = 0

	buf, _ := json.Marshal(plan)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {
		log.Println("err: ", err)
	}

}

func TestSchedulerO(minuteInDay int) {
	log.Println("testScheduler")
	type ScheduleInput struct {
		Uri         string `json:"uri"`
		ScheduleId  int    `json:"id"`
		MinuteInDay int    `json:"minuteInDay"`
		Loop        int    `json:"loop"`
		Action      int    `json:"action"`
	}

	schedule := ScheduleInput{}
	schedule.Uri = "/schedule"
	schedule.MinuteInDay = minuteInDay
	schedule.ScheduleId = randNum()
	schedule.Action = 1
	schedule.Loop = schedule.Loop | (1 << 2)

	buf, _ := json.Marshal(schedule)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {
		log.Println("err: ", err)
	}
}

func TestSchedulerC(minuteInDay int) {
	log.Println("testScheduler")
	type ScheduleInput struct {
		Uri         string `json:"uri"`
		ScheduleId  int    `json:"id"`
		MinuteInDay int    `json:"minuteInDay"`
		Loop        int    `json:"loop"`
		Action      int    `json:"action"`
	}

	schedule := ScheduleInput{}
	schedule.Uri = "/schedule"
	schedule.MinuteInDay = minuteInDay
	schedule.ScheduleId = randNum()
	schedule.Action = 0
	schedule.Loop = schedule.Loop | (1 << 2)

	buf, _ := json.Marshal(schedule)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {
		log.Println("err: ", err)
	}
}

func TestDelSchedule(id int) {
	log.Println("TestCancelSchedule")
	type DelSchedule struct {
		Uri        string `json:"uri"`
		ScheduleId int    `json:"id"`
	}

	delSchedule := DelSchedule{}
	delSchedule.Uri = "/delSchedule"
	delSchedule.ScheduleId = id

	buf, _ := json.Marshal(delSchedule)
	c := GetControllerById(cid)
	if c == nil {
		log.Printf("controller %s not exist", cid)
		return
	}

	err := c.SendMessage(buf)
	if err != nil {
		log.Println("err: ", err)
	}
}
